<?php
$title = '我的设置';
/** @var array $user */
/** @var array $accounts */
/** @var array|null $flash */
require __DIR__ . '/../partials/head.php';
require __DIR__ . '/../partials/nav.php';
?>
<section class="section">
    <div class="app-container" style="display:flex;flex-direction:column;gap:24px;">
        <?php if (!empty($flash)): ?>
            <div class="card" style="background:rgba(10,132,255,0.08);border:1px solid rgba(10,132,255,0.2);color:#0a84ff;">
                <?= htmlspecialchars($flash['message']) ?>
            </div>
        <?php endif; ?>
        <div class="card" style="gap:20px;">
            <h2 style="margin:0;">自动化设置</h2>
            <form method="post" action="/settings/toggle" style="display:flex;flex-direction:column;gap:18px;">
                <div style="display:flex;align-items:center;justify-content:space-between;">
                    <div>
                        <strong>允许 TradingView 自动化</strong>
                        <p style="color:var(--text-secondary);margin:4px 0;">关闭后将仅保留信号，不会下单。</p>
                    </div>
                    <label class="switch">
                        <input type="checkbox" name="tv_enabled" <?= $user['tv_enabled'] ? 'checked' : '' ?>>
                        <span class="slider"></span>
                    </label>
                </div>
                <div style="display:flex;align-items:center;justify-content:space-between;">
                    <div>
                        <strong>全局暂停</strong>
                        <p style="color:var(--text-secondary);margin:4px 0;">紧急情况下立即停止所有策略。</p>
                    </div>
                    <label class="switch">
                        <input type="checkbox" name="global_pause" <?= $user['global_pause'] ? 'checked' : '' ?>>
                        <span class="slider"></span>
                    </label>
                </div>
                <button class="btn" type="submit">保存设置</button>
            </form>
        </div>
        <div class="card" style="gap:20px;">
            <div style="display:flex;justify-content:space-between;align-items:center;">
                <h2 style="margin:0;">绑定交易所账户</h2>
                <button class="btn-ghost btn" data-toggle="#bind-modal" type="button">提交绑定</button>
            </div>
            <?php if (empty($accounts)): ?>
                <div class="empty-state">
                    <img src="/assets/img/empty/orders.svg" alt="暂无账户">
                    <p>暂未在 hummingbot-api 中检测到账户，请联系运维创建。</p>
                </div>
            <?php else: ?>
                <table class="table">
                    <thead>
                    <tr>
                        <th>账户名</th>
                        <th>Connector</th>
                        <th>状态</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($accounts as $acct): ?>
                        <tr>
                            <td><?= htmlspecialchars($acct['account_name'] ?? '-') ?></td>
                            <td><?= htmlspecialchars($acct['connector'] ?? '-') ?></td>
                            <td><?= htmlspecialchars($acct['status'] ?? 'N/A') ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>
        <div class="card" style="gap:18px;">
            <h2 style="margin:0;">安全与通知</h2>
            <p style="color:var(--text-secondary);margin:0;">即将上线：邮箱通知、Webhook 通知、2FA 设置中心。当前如需修改密码，请联系管理员。</p>
        </div>
    </div>
</section>
<div class="modal-backdrop" id="bind-modal">
    <div class="modal">
        <h3>提交账户绑定请求</h3>
        <p style="color:var(--text-secondary);">信息将记录至审计，运维在 hummingbot-api 中完成凭据配置后自动生效。</p>
        <form method="post" action="/settings/bind" style="display:flex;flex-direction:column;gap:14px;">
            <div class="field">
                <label>账户名称</label>
                <input type="text" name="account_name" required>
            </div>
            <div class="field">
                <label>API Key</label>
                <input type="text" name="api_key" required>
            </div>
            <div class="field">
                <label>API Secret</label>
                <input type="password" name="api_secret" required>
            </div>
            <div style="display:flex;gap:12px;justify-content:flex-end;">
                <button class="btn-ghost btn" type="button" data-toggle="#bind-modal">取消</button>
                <button class="btn" type="submit">提交</button>
            </div>
        </form>
    </div>
</div>
<?php require __DIR__ . '/../partials/footer.php'; ?>
